<template>
  <!-- :destroyOnClose="true" -->
  <a-modal
    :title="`用户缩减${model && model.id > 0 ? '编辑' : '新增'}`"
    :width="680"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 4 } }" :wrapperCol="{ sm: { span: 19 } }" :selfUpdate="true">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <!-- <a-form-item label="范围">
          <a-input
            v-decorator="['name', { rules: [{ required: true, message: '财务名称不能为空' }] }]"
            placeholder="请输入名称"
          />
        </a-form-item> -->
        <!-- <a-row>
          <a-col :span="11">
            
          </a-col>
          <a-col :span="1">-</a-col>
          <a-col :span="11"></a-col>
        </a-row> -->
        <a-form-item label="范围">
          <!-- <a-input v-decorator="['amount', { rules: [{ required: true, message: '请输入金额数' }] }]" /> -->
          <a-input-number
            class="awm"
            :min="0"
            v-decorator="['fromNum', { rules: [{ required: true, message: '请输入开始范围' }] }]"
            placeholder="请输入开始范围"
          />
          -
          <a-input-number
            class="awm"
            :min="0"
            v-decorator="['endNum', { rules: [{ required: true, message: '请输入结束范围' },{ required: true, validator: validateToEndNum}] }]"
            placeholder="请输入结束范围"
          />
        </a-form-item>
        <a-form-item label="缩减比例(%)">
          <a-input-number
            style="width: 100%"
            class="awm"
            :min="0"
            :max="100"
            v-decorator="['rate', { rules: [{ required: true, message: '请输入缩减比例' }] }]"
            placeholder="请输入缩减比例(0-100)"
          />
        </a-form-item>
        <a-form-item label="用户">
          <AWM-select
            v-decorator="['userId', { rules: [{ required: true, message: '用户不能为空' }] }]"
            placeholder="请输入选择用户"
            funStr="userListByNoAuth"
          ></AWM-select>
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <a-button
        @click="
          () => {
            $emit('cancel')
          }
        "
        >取消</a-button
      >
      <a-button
        @click="
          () => {
            $emit('ok', 2)
          }
        "
        type="primary"
        :spinning="loading"
        >提交</a-button
      >
    </template>
  </a-modal>
</template>

<script>
import moment from 'moment'
import pick from 'lodash.pick'
import cloneDeep from 'lodash.clonedeep'
import { mapGetters } from 'vuex'
import { uploadForQiniu } from '@/utils/qiniu'
import { userListByNoAuth } from '@/api/noAuth'
import AWMSelect from '@/components/AWMSelect'
// 表单字段
const fields = ['id', 'fromNum', 'endNum', 'rate', 'userId']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    typeList: {
      type: Array,
      default: () => [],
    },
    model: {
      type: Object,
      default: () => null,
    },
    financeTypeList: {
      type: Array,
      default: () => [],
    },
    showSave: {
      type: Boolean,
      default: true,
    },
    type: {
      type: Number,
      default: 1,
    },
  },
  components: {
    'AWM-select': AWMSelect,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      f_typeList: [],
      fileList: [],
      uid: null,
      user: {
        fetching: false,
        timeout: null,
        list: [],
      },
    }
  },
  created() {
    let that = this
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      let data = pick(this.model, fields)
      console.log(this.model)
      if (this.model) {
        this.form.setFieldsValue(data)
      }
    })
  },
  methods: {
    validateToEndNum(rule, value, callback) {
      const self = this;
      const form = self.form;
      const { fromNum, endNum } = form.getFieldsValue()
      console.log(fromNum, endNum);
      if(!fromNum){
        callback()
      }
      if(fromNum > endNum){
        console.log(111);
        callback(new Error('结束范围必须大于开始范围'))
      }
      callback()
    },
  },
  computed: {
    ...mapGetters({ baseUrl: 'baseUrl' }),
  },
}
</script>
<style scoped lang="less">
</style>
